using System;
using System.Collections;
using System.Collections.Generic;

namespace WindowsPhone.CompactContainer.ServiceLocation.CompactContainer
{
    /// <summary>
    /// 
    /// </summary>
    public class ComponentCollection : IEnumerable<ComponentInfo>
    {
        private readonly List<ComponentInfo> _list = new List<ComponentInfo>();
        
        /// <summary>
        /// Gets the total number of components.
        /// </summary>
        public int Count
        {
            get
            {
                return _list.Count;
            }
        }

        /// <summary>
        /// Gets the singletons count.
        /// </summary>
        /// <value>The singletons count.</value>
        public int SingletonsCount { get; private set; }

        /// <summary>
        /// Gets the <see cref="ComponentInfo"/> at the specified index.
        /// </summary>
        /// <value></value>
        public ComponentInfo this[int index]
        {
            get { return _list[index]; }
            set { _list[index] = value;}
        }

        /// <summary>
        /// Indexes the of.
        /// </summary>
        /// <param name="ci">The ci.</param>
        /// <returns></returns>
        public int IndexOf(ComponentInfo ci)
        {
            return _list.IndexOf(ci);
        }

        /// <summary>
        /// Adds the specified component info.
        /// </summary>
        /// <param name="ci">The ci.</param>
        public void Add(ComponentInfo ci)
        {
            if (ci.Lifestyle == LifestyleType.Singleton)
            {
                SingletonsCount++;
            }

            _list.Add(ci);
        }

        /// <summary>
        /// Removes the specified ci.
        /// </summary>
        /// <param name="ci">The ci.</param>
        /// <returns></returns>
        public bool Remove(ComponentInfo ci)
        {
            if (ci.Lifestyle == LifestyleType.Singleton)
            {
                SingletonsCount--;
            }

            return _list.Remove(ci);
        }

        public List<ComponentInfo> GetComponentInfoListFor(Type serviceType)
        {
            List<ComponentInfo> results = new List<ComponentInfo>(); 
            foreach (ComponentInfo component in _list)
            {
                if (component.ServiceType == serviceType) 
                    results.Add(component);
            } 
            return results;
        }    
        
        public ComponentInfo FindForService(Type serviceType)
        {
            ComponentInfo result = null; 
            foreach (ComponentInfo component in _list)
            {
                if (component.ServiceType == serviceType)
                {
                    result = component; break;
                }
            } 
            return result;
        }
        
        public ComponentInfo FindForClass(Type classType)
        {
            ComponentInfo result = null; 
            foreach (ComponentInfo component in _list)
            {
                if (component.ClassType.Equals(classType))
                {
                    result = component; break;
                }
            } 
            return result;
        }
        
        public ComponentInfo FindKey(string key)
        {
            ComponentInfo result = null; 
            foreach (ComponentInfo component in _list)
            {
                if (component.Key.Equals(key))
                {
                    result = component; break;
                }
            } 
            return result;
        }

        /// <summary>
        /// Gets the enumerator.
        /// </summary>
        /// <returns></returns>
        public IEnumerator<ComponentInfo> GetEnumerator()
        {
            return _list.GetEnumerator();
        }

        /// <summary>
        /// Gets the enumerator.
        /// </summary>
        /// <returns></returns>
        IEnumerator IEnumerable.GetEnumerator()
        {
            return ((IEnumerable)_list).GetEnumerator();
        }
    }
}